package cn.wolfcode.wolf2w.service.imp;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;

import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.awt.*;
import java.util.*;
import java.util.List;

@Service
//@Transactional  暂时先别管事务
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository repository;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IRegionService regionService;

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    public Destination get(String id) {
        Optional<Destination> optional = repository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    /**
     * 分页查询
     *
     * @param qo
     * @return
     */
    @Override
    public Page<Destination> query(DestinationQuery qo) {
        Query query = new Query();

        if (StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        if (StringUtils.hasLength(qo.getParentId())) {
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        } else {
            query.addCriteria(Criteria.where("parentId").is(null));
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template, Destination.class, query, pageable);
    }

    //page对象里面包括: 总页数 每页显示条数......

    public List<Destination> list() {
        return repository.findAll();
    }


    @Override
    public void saveOrUpdate(Destination destination) {
        if (!StringUtils.hasLength(destination.getId())) {
            this.save(destination);
        } else {
            this.update(destination);
        }

    }

    /**
     * 保存
     *
     * @param destination
     */
    @Override
    public void save(Destination destination) {
        destination.setId(null);//把id设置为null
        repository.save(destination);//存入destination对象
    }

    /**
     * 更新
     *
     * @param destination
     */
    @Override
    public void update(Destination destination) {
        repository.save(destination);//编辑和保存是一样的,更新之后则要保存destination对象
    }

    /**
     * 创建吐司
     *
     * @param list
     * @param parentId
     */
    //创建toast
    @Override
    public void createToast(List<Destination> list, String parentId) {
        if (!StringUtils.hasLength(parentId)) {
            return;
        }
        Destination destination = this.get(parentId);
        list.add(destination);
        if (StringUtils.hasLength(destination.getParentId())) {
            createToast(list, destination.getParentId());
        }
    }

    /**
     * 吐司查询
     *
     * @param parentId
     * @return
     */
    @Override
    public List<Destination> queryToasts(String parentId) {
        List<Destination> list = new ArrayList<>();
        this.createToast(list, parentId);
        Collections.reverse(list);
        return list;
    }


    @Override
    public List<Destination> queryDestByRegionIdForApi(String regionId) {
        List<Destination> list = new ArrayList<>();

        //第一层
        if ("-1".equals(regionId)) {
            list = repository.findByParentName("中国");

        } else {
            list = this.queryDestByRegionId(regionId);
        }

        //第二层
        for (Destination destination : list) {
            Pageable pageable = PageRequest.of(0, 5, Sort.Direction.ASC, "_id");
            List<Destination> children = repository.findByParentId(destination.getId(), pageable);
            destination.setChildren(children);
        }


        return list;
    }

    @Override
    public List<Destination> queryDestByRegionId(String regionId) {
        //通过区域id查询区域对象
        Region region = regionService.get(regionId);
        //获取目的地挂载的目的地id集合
        List<String> ids = region.getRefIds();
        return repository.findByIdIn(ids);
    }
}
